/*
package com.bing.bingPro.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.org.atool.fluent.mybatis.base.crud.IBaseQuery;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.bing.bingPro.entity.TestCcMail;
import com.bing.bingPro.entity.TestDetailsEntity;
import com.bing.bingPro.entity.VendorEmailEntity;
import com.bing.bingPro.mapper.TestCcMailMapper;
import com.bing.bingPro.mapper.TestDetailsMapper;
import com.bing.bingPro.mapper.VendorEmailMapper;
import com.bing.bingPro.util.ExcelData;
import com.bing.bingPro.util.RedisUtil;
import com.bing.bingPro.wrapper.TestCcMailQuery;
import com.bing.bingPro.wrapper.TestDetailsQuery;
import com.bing.bingPro.wrapper.VendorEmailQuery;
import com.cmcc.jk.common.mail.client.dto.MailClientAttachment;
import com.cmcc.jk.common.mail.client.dto.MailClientInSend;
import com.cmcc.jk.common.mail.client.dto.MailClientInternetAddress;
import com.cmcc.jk.common.mail.client.service.IMailClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TestDetailsConfigService {


    private final SendMailService sendMailService;
    private final TestDetailsMapper testDetailsMapper;

    private final VendorEmailMapper vendorEmailMapper;

    private final TestCcMailMapper testCcMailMapper;
    @Autowired
    private IMailClientService mailClientService;

    public TestDetailsConfigService(SendMailService sendMailService, TestDetailsMapper testDetailsMapper, VendorEmailMapper vendorEmailMapper, TestCcMailMapper testCcMailMapper) {
        this.sendMailService = sendMailService;
        this.testDetailsMapper = testDetailsMapper;
        this.vendorEmailMapper = vendorEmailMapper;
        this.testCcMailMapper = testCcMailMapper;
    }

    @Resource
    private RedisUtil redisUtil;
    public String DATALIST = "datalist";
    public String TOTAL = "total";
    public String IDLIST = "idList";
    public String T2021VERSION = "2021年统一固件软件版本";
    public String T2022VERSION = "2022年统一固件软件版本";

    public Date createDate() throws ParseException {
        LocalDateTime date = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 获取当前日期
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return fmt.parse(date.format(formatter));
    }


    // 导入全量表数据，根据省份，网关型号，2022版本进行更新或新增
    public String newSetDataTableFunc(MultipartFile file) throws IOException, ParseException {

        // 获取文件
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<Map<String, Object>> maps = reader.readAll();
        int newLine = 0;
        int updateLine = 0;
        for (Map<String, Object> mapObj : maps) {
            int count = testDetailsMapper.count(testDetailsMapper.query()
                    .where.province().eq("内蒙古".equals(mapObj.get("省份")) ? "内蒙" : mapObj.get("省份").toString())
                    .and.vendor().eq(mapObj.get("厂商"))
                    .and.t2022Version().eq(mapObj.get(T2022VERSION))
                    .and.type().eq(mapObj.get("类型"))
                    .and.wifiChipFactory().eq(mapObj.get("WiFi芯片厂家"))
                    .and.gatewayModel().eq(mapObj.get("网关型号"))
                    .and.gatewayHardwareVersion().eq(mapObj.get("网关硬件版本")).end());
            // 存在条目
            if (count > 0) {
                int i = testDetailsMapper.updateBy(testDetailsMapper.updater()
                        .set.shape().is(mapObj.get("形态"))
                        .set.type().is(mapObj.get("类型"))
                        .set.chipFactory().is(mapObj.get("芯片厂家"))
                        .set.cpuModel().is(mapObj.get("CPU型号"))
                        .set.wifiChipModel().is(mapObj.get("WiFi芯片型号"))
                        .set.wifiChipFactory().is(mapObj.get("WiFi芯片厂家"))
                        .set.ramModel().is(mapObj.get("内存大小"))
                        .set.t2023Version().is(mapObj.get("2023年统一固件软件版本"))
                        .set.vdContact().is(mapObj.get("申请人"))
                        .set.vdContactMailbox().is(mapObj.get("邮箱"))
                        .set.vdContactMobile().is(mapObj.get("联系方式")).end()
                        .where.province().eq("内蒙古".equals(mapObj.get("省份").toString()) ? "内蒙" : mapObj.get("省份"))
                        .and.vendor().eq(mapObj.get("厂商"))
                        .and.t2022Version().eq(mapObj.get(T2022VERSION))
                        .and.gatewayModel().eq(mapObj.get("网关型号"))
                        .and.gatewayHardwareVersion().eq(mapObj.get("网关硬件版本")).end());
                Console.log(mapObj.get("省份").toString(), " 更新 ", mapObj.get("网关硬件版本").toString(), "状态：", i);
                updateLine += i;
            } else {
                TestDetailsEntity entity = new TestDetailsEntity();
                entity.setVendor(mapObj.get("厂商").toString());
                entity.setShape(mapObj.get("形态").toString());
                entity.setType(mapObj.get("类型").toString());
                entity.setGatewayModel(mapObj.get("网关型号").toString());
                entity.setGatewayHardwareVersion(mapObj.get("网关硬件版本").toString());
                entity.setChipFactory(mapObj.get("芯片厂家").toString());
                entity.setCpuModel(mapObj.get("CPU型号").toString());
                entity.setWifiChipFactory(mapObj.get("WiFi芯片厂家").toString());
                entity.setWifiChipModel(mapObj.get("WiFi芯片型号").toString());
                entity.setRamModel(mapObj.get("内存大小").toString());
                entity.setProvince("内蒙古".equals(mapObj.get("省份").toString()) ? "内蒙" : mapObj.get("省份").toString());
                entity.setVdContact(mapObj.get("申请人").toString());
                entity.setVdContactMailbox(mapObj.get("邮箱").toString());
                entity.setVdContactMobile(mapObj.get("联系方式").toString());
                entity.setT2023Version(mapObj.get("2023年统一固件软件版本").toString());
                entity.setCreateTime(createDate());
                int j = testDetailsMapper.insert(entity);
                Console.log(mapObj.get("省份").toString(), " 新增 ", mapObj.get("网关硬件版本").toString(), "状态：", j);
                newLine += j;
            }

        }
        return String.format("全量数据导入状态：新增 %s 条数据，更新 %s 条数据。", newLine, updateLine);
    }


    public void testSendEmailJob() {
        boolean lock = redisUtil.setNx("testSendEmailJob", 10800);
        if(!lock){
            log.info("跳过本次");
            return;
        }
        IBaseQuery<TestCcMail, TestCcMailQuery> testCcMailQuery = testCcMailMapper.query()
                .select.id().name().ccMail().end();
        List<String> testCcMails = testCcMailMapper.listEntity(testCcMailQuery).stream().map(TestCcMail::getCcMail)
                .collect(Collectors.toList());
        VendorEmailQuery ew = new VendorEmailQuery();
        List<VendorEmailEntity> vendorEmailEntities = vendorEmailMapper.listEntity(ew);
        vendorEmailEntities.forEach(vendorEmailEntity -> {

            String vendor = vendorEmailEntity.getVendor();
            String vdContactMailbox = vendorEmailEntity.getVdContactMailbox();
            IBaseQuery<TestDetailsEntity, TestDetailsQuery> testDetailsQueryIBaseQuery = testDetailsMapper.query()
                    .select.vendor().shape().type().foundry().gatewayModel().gatewayHardwareVersion()
                    .chipFactory().cpuModel().wifiChipFactory().wifiChipModel().ramModel().t2023Version()
                    .province().t2023TestTimestamp().t2023TotalPassRate().t2023AllowDeploy()
                    .t2023Result().end()
                    .where.vendor().eq(vendor)
                    .and.t2023Version().notNull().end();
            List<TestDetailsEntity> testDetailsEntities = testDetailsMapper.listEntity(testDetailsQueryIBaseQuery);
            if (CollUtil.isNotEmpty(testDetailsEntities)) {
                try {
                    log.info("准备发送邮件:{}", testDetailsEntities.get(0).getVendor());
                    ExcelData excelData = new ExcelData(TestDetailsEntity.class, testDetailsEntities);
                    byte[] bytes = convertExcelDataToByteArray(excelData);
                    senVendorMail(bytes, vendor,vdContactMailbox,testCcMails);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt(); // 重新中断线程
                    log.error("线程被中断", e);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });
    }


    private synchronized void senVendorMail(byte[] bytes, String vendor,String vdContactMailbox,List<String> testCcMails) throws InterruptedException {
        final String subject = "总部家客运维系统2023年统一软件固件版本测试情况";
        final String contentPlainText = "您好，" + "\n"
                + "截至目前，总部家客运维系统2023年统一软件固件版本测试情况，" +
                "请您参考附件内容。如有问题请直接与总部系统联系人朱亦然(18255179471,zhuyiran@cmdi.chinamobile.com)沟通。该邮件为自动发送，请不要直接回复该邮件！";
        // 收件人
        final MailClientInternetAddress to = MailClientInternetAddress.builder().mailAddress(vdContactMailbox).build();
        //抄送人
        List<MailClientInternetAddress> ccList = testCcMails.stream()
                .map(ccMail -> MailClientInternetAddress.builder().mailAddress(ccMail).build())
                .collect(Collectors.toList());
        final List<MailClientInternetAddress> toList = Arrays.asList(to);
        // 邮件附件
        final MailClientAttachment attachment = MailClientAttachment.builder() //
                .filename(vendor + "-2023年统一软件固件版本测试情况.xlsx") // 附件在邮件中显示的名称
                .content(bytes) // 文件内容，字节数组
                .build();
        final List<MailClientAttachment> attachmentList = Arrays.asList(attachment);
        // 完整参数
        final MailClientInSend mailClientInSend = MailClientInSend.builder() //
                .to(toList) //收件人
                .cc(ccList) //抄送人
                .subject(subject) //邮件主题
                .contentPlainText(contentPlainText) //邮件内容
                .attachment(attachmentList) //邮件附件
                .build();
        mailClientService.send(mailClientInSend);
        log.info("发送成功");
    }

    public static byte[] convertExcelDataToByteArray(ExcelData excelData) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ExcelWriter excelWriter = EasyExcelFactory.write(outputStream).build();

        // 创建一个写入表单
        WriteSheet writeSheet = EasyExcelFactory.writerSheet(0, excelData.getSheetName()).head(excelData.getClazz()).build();

        // 写入数据
        excelWriter.write(excelData.getData(), writeSheet);

        // 关闭 ExcelWriter
        excelWriter.finish();

        // 将 ByteArrayOutputStream 中的数据转换为字节数组
        byte[] byteArray = outputStream.toByteArray();

        // 关闭 ByteArrayOutputStream
        outputStream.close();

        return byteArray;
    }

}
*/
